package com.example.demo.service.Impl;


import com.example.demo.mapper.EmpMapper;
import com.example.demo.pojo.Emp;
import com.example.demo.pojo.PageBean;
import com.example.demo.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {


    @Autowired
    private EmpMapper empMapper;
    @Override
    public PageBean selectEmpsByCondition(String name, Byte gender, LocalDate begin, LocalDate end, int page, int pageSize) {
        //调用 数据层 Mapper层
        // 让Mapper层 根据 name gender begin end  查询出 总数量
        long total = empMapper.selectEmpsCountByCondition(name ,gender, begin, end);
        // 让Mapper层 根据 name gender begin end  查询出数据  并且要根据 page pageSize 查询出第一页数据，
        // order by updateTime desc
        int startIndex = (page-1)*pageSize;
        List<Emp> rows = empMapper.selectEmpsByCondition(name,gender,begin,end,startIndex,pageSize);

        PageBean pb= new PageBean(total,rows);

        return pb;
    }

    @Override
    public List<Emp> deleteEmpsById(Integer[] ids) {
        List<Emp> list=empMapper.deleteEmpsById(ids);
        return list;
    }

    @Override
    public int insertEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        int row=empMapper.insertEmps(emp);
        return row;
    }

    @Override
    public int updateEmps(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        int row=empMapper.updateEmps(emp);
        return row;
    }

    @Override
    public Emp selectById(Integer id) {
        Emp emp1=empMapper.selectById(id);
        return emp1;
    }


}
